package com.oa.co.controller;
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.oa.co.bo.Business_tripBo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.listener.Business_tripListener;
import com.oa.co.pojo.Business_trip;
import com.oa.co.service.BusinessTripService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author sunXuDong
 * @data 2023/5/8
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/api/btc")
public class BusinessTripController {
    @Autowired
    private BusinessTripService businessTripService;
    //查询指定类型数据
    //province项目类型
    @RequestMapping("/queryAll")
    public R queryAll(Integer province){

        return businessTripService.queryAll(province);
    }
    //查询指定项目名称
    //proname项目名称
    @RequestMapping("/queryProname")
    public R queryProname(String proname){

        return businessTripService.queryProname(proname);
    }
    //查询指定类型数据
    //province项目类型
    @RequestMapping("/queryApplicant")
    public R queryApplicant(Integer province){

        return businessTripService.queryApplicant(province);
    }
    //查询所有数据
    //分页查询
    @RequestMapping("/btcList")
    public PageVo<Business_trip> btcList(Integer page, Integer limit, String proname, Integer t_id
            , String place_of_departure, String arrival_point){
        return businessTripService.btcList(page,limit,proname,t_id,place_of_departure,arrival_point);
    }
    //删除
    @GetMapping("/deleteById")
    public R deleteById(Integer id){

        return businessTripService.deleteById(id);
    }
    //编辑
    @PostMapping("/update")
    public R update(@RequestBody Business_trip business_trip){

        return businessTripService.update(business_trip);
    }
    //添加
    @PostMapping("/add")
    public R add(@RequestBody Business_trip business_trip){

        return businessTripService.add(business_trip);
    }
    //柱图
    @PostMapping("/bar")
    public R bar(){

        return businessTripService.bar();
    }
    //饼图1
    @PostMapping("/pPie")
    public R pPie(){

        return businessTripService.pPie();
    }
    //饼图2  3
    @PostMapping("/aPie")
    public R aPie(){

        return businessTripService.aPie();
    }
    //导出功能
    @RequestMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response, Integer page, Integer limit, Integer t_id, String proname,String place_of_departure,String arrival_point) throws IOException {

        //下载 excel  模板文件
        //设置下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");


        //防止中文乱码
        String filename = URLEncoder.encode("项目信息导出", "utf-8");

        //告诉浏览器  你要下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到一个 指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, Business_tripBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        PageInfo<Business_tripBo> info = businessTripService.btcListBo
                (page,limit,proname,t_id,place_of_departure,arrival_point);

        sheet.doWrite(info.getList());


    }

    @Autowired
    private Business_tripListener business_tripListener;
    //上传文件批量添加
    @RequestMapping("/import")
    public R btImport(MultipartFile file) throws IOException {

        ExcelReaderBuilder builder = EasyExcel.read(file.getInputStream(),Business_tripBo.class, business_tripListener);

        ExcelReaderSheetBuilder sheet = builder.sheet();

        sheet.doRead();

        return R.ok();
    }
    //下载 excel  模板文件
    @RequestMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //Business_tripListener.xlsx
        //下载 excel  模板文件
        //设置下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 得到这个文件的真实路径
        String realPath = request.getServletContext().getRealPath("/Business_tripListener.xlsx");
        File file = new File(realPath);
        //借助 commons-io 的 工具类 把 file 对象转为 字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止中文乱码
        String filename = URLEncoder.encode("项目信息模板", "utf-8");

        //告诉浏览器  你要下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到一个 指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        //输出流 把字节数组 输出到 浏览器
        outputStream.write(bytes);
        //关闭流
        outputStream.close();
    }
}
